import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";

@IonicPage({
    segment: 'information/edit/:kind/:type_id/:amoy_id',
    defaultHistory: ['MemberPage', 'PersonMessagePage']
})
@Component({
    selector: 'page-information-edit',
    templateUrl: 'information-edit.html',
})
export class InformationEditPage {
    options: any;
    type_name: any;
    template: any;
    infoData = [];
    infoBuildData = [];
    infoIdData = [];
    auto_content = '';
    checkStatus = [];
    infoTextData = [];
    //需要发送的数据
    sendData = {
        amoy_id: 0,
        auto: '',
        role: 1,
        kind: 'information',
        type_id: this.navParams.get('type_id'),
        click_pay: 0,
        reward: 0,
        cash_deposit: 0,
        see_pay: 0,
        data: ""
    };
    editStatus = {};

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewDidLoad() {
        this.getOptions();
    }

    //获取表单数据
    getOptions() {
        let type_id = this.navParams.get('type_id');
        let kind = this.navParams.get('kind');
        this.httpService.requestData('/task_info/get_option/type_id/' + type_id + '/kind/' + kind, data => {
            this.options = data['data']['data'];
            this.type_name = data['data']['type_name'];
            this.template = data['data']['tpl'];
            let amoy_id = this.navParams.get('amoy_id');
            if (amoy_id) {
                this.sendData.amoy_id = amoy_id;
                this.getInfo(amoy_id);
            }
        });
    }

    /**
     * 获取数据
     * */
    getInfo(amoy_id) {
        let apiUrl = "/task_info/edit_data_option_old/amoy_id/" + amoy_id;
        this.httpService.requestData(apiUrl, response => {
            let data = response['data'];
            this.editStatus = {
                see_pay: response['see_pay'],
                cash_deposit: response['cash_deposit'],
                click_pay: response['click_pay'],
                reward: response['reward']
            };
            this.sendData.role = response['role'];
            this.sendData.see_pay = response['see_pay'];
            this.sendData.cash_deposit = response['cash_deposit'];
            this.sendData.click_pay = response['click_pay'];
            this.sendData.reward = response['reward'];
            for (let item in data) {
                for (let index in data[item]) {
                    if (!this.util.isArray(this.infoData[data[item][index]['letter']])) {
                        this.infoData[data[item][index]['letter']] = [];
                    }
                    if (!this.util.isArray(this.infoBuildData[data[item][index]['num']])) {
                        this.infoBuildData[data[item][index]['num']] = [];
                    }
                    if (!this.util.isArray(this.infoIdData[data[item][index]['num']])) {
                        this.infoIdData[data[item][index]['num']] = [];
                    }
                    if (data[item][index]['option_type'] == 0 || data[item][index]['option_type'] == 1) {
                        this.infoData[data[item][index]['letter']].push(data[item][index]['option_list'][0]);
                        this.infoBuildData[data[item][index]['num']].push(data[item][index]['option_list'][0]);
                        this.infoIdData[data[item][index]['num']].push(data[item][index]['option_list'][0]);
                    } else if (data[item][index]['option_type'] == 2 || data[item][index]['option_type'] == 3) {
                        for (let option in data[item][index]['option_list']) {
                            if (data[item][index]['option_list'][option]['selected'] == 1) {
                                this.infoData[data[item][index]['letter']].push(data[item][index]['option_list'][option]['list_name']);
                                this.infoBuildData[data[item][index]['num']].push(data[item][index]['option_list'][option]['list_name']);
                                this.infoIdData[data[item][index]['num']].push(data[item][index]['option_list'][option]['list_id']);
                            }
                        }
                    } else if (data[item][index]['option_type'] == 5) {
                        this.infoData[data[item][index]['letter']] = data[item][index]['option_list'];
                        this.infoBuildData[data[item][index]['num']] = data[item][index]['option_list'];
                        this.infoIdData[data[item][index]['num']] = data[item][index]['option_list'];
                    } else if (data[item][index]['option_type'] == 6) {
                        this.infoData[data[item][index]['letter']] = data[item][index]['option_list'][0];
                        this.infoBuildData[data[item][index]['num']] = data[item][index]['option_list'][0];
                        this.infoIdData[data[item][index]['num']] = data[item][index]['option_list'][0];
                    }
                }
            }
            this.buildContent();
        });
    }

    //选择多选数据
    selCheckData(item, option) {
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        if (this.util.inArray(this.infoData[item.letter], option.list_name) < 0) {
            this.infoData[item.letter].push(option.list_name);
            this.infoBuildData[item.num].push(option.list_name);
            this.infoIdData[item.num].push(option.list_id);
        } else {
            let index = this.util.inArray(this.infoData[item.letter], option.list_name);
            if (index !== -1) {
                this.infoData[item.letter].splice(index, 1);
                this.infoBuildData[item.num].splice(index, 1)
                this.infoIdData[item.num].splice(index, 1)
            }
        }
        this.buildContent();
    }

    //单选数据
    selRadioData(item, option) {
        this.infoData[item.letter] = option.list_name;
        this.infoBuildData[item.num] = option.list_name;
        this.infoIdData[item.num] = option.list_id;
        this.infoTextData[item.letter] = '';
        this.buildContent();
    }

    //文本框数据
    textData(item) {
        // this.infoData[item.letter] = this.infoTextData[item.letter];
        this.infoBuildData[item.num] = this.infoData[item.letter];
        this.infoIdData[item.num] = this.infoData[item.letter];
        this.buildContent();
    }

    //多选文本框数据
    checkFocusData(item) {
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        let index = this.util.inArray(this.infoData[item.letter], this.infoTextData[item.letter]);
        if (index !== -1) {
            this.infoData[item.letter].splice(index, 1);
            this.infoBuildData[item.num].splice(index, 1);
            this.infoIdData[item.num].splice(index, 1);
        }
        this.buildContent();
    }

    checkBlurData(item) {
        if (this.infoTextData[item.letter] == undefined || this.infoTextData[item.letter] == '') {
            return false;
        }
        if (!this.util.isArray(this.infoData[item.letter])) {
            this.infoData[item.letter] = [];
        }
        if (!this.util.isArray(this.infoBuildData[item.num])) {
            this.infoBuildData[item.num] = [];
        }
        if (!this.util.isArray(this.infoIdData[item.num])) {
            this.infoIdData[item.num] = [];
        }
        this.infoData[item.letter].push(this.infoTextData[item.letter]);
        this.infoBuildData[item.num].push(this.infoTextData[item.letter]);
        this.infoIdData[item.num].push(this.infoTextData[item.letter]);
        this.buildContent();
    }

    //自动生成文本内容
    buildContent() {
        this.auto_content = this.template;
        this.infoBuildData.forEach((ele, index) => {
            let reg = new RegExp('\\[' + index + '\\]', "g");
            if (this.util.isArray(ele)) { //多选
                if (ele.length > 0) {
                    this.auto_content = this.auto_content.replace(reg, ele.join(','));
                }
            } else {
                if (ele != '') {
                    this.auto_content = this.auto_content.replace(reg, ele);
                }
            }
        });
    }

    selDate(item, event){
        let year = event.year;
        let month = event.month < 10 ? '0' + event.month : event.month;
        let day = event.day;
        if(this.infoData[item.letter] == '' || this.infoData[item.letter] == undefined){
            return;
        }
        this.infoBuildData[item.num] = year + '年' + month + '月' + day + '日';
        this.infoIdData[item.num] = this.infoData[item.letter];
        this.buildContent();
    }

    resetData() {
        this.infoData = [];
        this.infoBuildData = [];
        this.infoTextData = [];
        this.buildContent();
    }

    uploadImage(item, e) {
        let file = e.target.files[0];
        let data = new FormData();
        data.append('image', file);
        data.append('type', 'moments');
        this.httpService.uploadImage('/user/upload_file', data, result => {
            this.infoData[item.letter].push(result.img_path);
            this.infoBuildData[item.num] = this.infoData[item.letter];
            this.infoIdData[item.num] = this.infoData[item.letter];
        });
    }

    delImage(item, value) {
        this.util.presentConfirm('确定删除吗？', () => {
            let index = this.util.inArray(this.infoData[item.letter], value);
            if (index > -1) {
                this.infoData[item.letter].splice(index, 1);
            }
        });
    }

    submitData() {
        this.sendData.auto = this.auto_content;
        this.sendData.data = JSON.stringify(this.infoIdData);
        let apiUrl = "/task_info/reply_edit_data";
        this.httpService.requestPostData(apiUrl, this.sendData, data => {
            if(data.code == 200){
                this.util.presentAlert('信息修改成功');
                this.navCtrl.pop();
            }else{
                this.util.presentAlert('信息修改失败，请稍后再试！');
            }
        })
    }

}
